﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IHotelDal;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Model;

namespace HotelDal
{
    public class GetOrderDal: IGetOrderDal
    {
        MyDbcontext Dbcontext = new MyDbcontext();

        /// <summary>
        /// 新增客房订单
        /// </summary>
        /// <param name="Gorder"></param>
        /// <returns></returns>
        public async Task<ActionResult<int>> AddRoomOrder(Guestroomorder Gorder)
        {
            Dbcontext.Guestroomorder.Add(Gorder);
            return await Dbcontext.SaveChangesAsync();
        }

        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public ActionResult<Guestroomorder> DataSingle(int oid)
        {
            return Dbcontext.Guestroomorder.Find(oid);
        }

        /// <summary>
        /// 删除客房订单
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public async Task<ActionResult<int>> DeleteOrder(int oid)
        {
            var id = Dbcontext.Guestroomorder.Find(oid);
            Dbcontext.Guestroomorder.Remove(id);
            return await Dbcontext.SaveChangesAsync();
        }

        /// <summary>
        /// 获取所有订单
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult<IEnumerable<Guestroomorder>>> GetOrder(string user)
        {
            if (!string.IsNullOrEmpty(user))
            {
                return await Dbcontext.Guestroomorder.Include("Guestroomtype").Where(s => s.AUserName.Contains(user)).ToListAsync();
            }

            return await Dbcontext.Guestroomorder.Include("Guestroomtype").ToListAsync();
        }

        /// <summary>
        /// 获取客房类型
        /// </summary>
        /// <returns></returns>
        public async Task<ActionResult<IEnumerable<Guestroomtype>>> GetOrderoomType()
        {
            return await Dbcontext.Guestroomtypes.ToListAsync();
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public async Task<ActionResult<int>> UpdateOrder(Guestroomorder guestroomorder)
        {
            Dbcontext.Entry(guestroomorder).State = EntityState.Modified;
            return await Dbcontext.SaveChangesAsync();
        }

    }
}
